package cfca.seal.web.action.manager;

import cfca.seal.bean.AdminUser;
import cfca.seal.bean.OperationLog;
import cfca.seal.common.Page;
import cfca.seal.exception.CodeException;
import cfca.seal.mananger.OperationLogManager;
import cfca.seal.util.PwdEncryptUtil;
import cfca.seal.util.StringUtil;
import cfca.seal.web.service.ManagerService;
import cfca.seal.web.service.OperationLogService;
import cfca.seal.web.system.SessionManager;
import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping({"/manager"})
public class ManagerAction
{
  private String jumpListPagePath = "sys/manager/list.html";
  private String jumpDetailPagePath = "sys/manager/detail.html";
  private String jumpEditPagePath = "sys/manager/edit.html";
  private String jumpEditPwdPagePath = "sys/manager/passwordEdit.html";
  private String jumpAddPagePath = "sys/manager/add.html";
  private String jumplistdo = "redirect:/manager/v_list.do";
  private String applicationName = "管理员管理";

  @Resource
  private ManagerService managerService;

  @Resource
  private OperationLogService operationLogService;
  private static final Logger logger = LoggerFactory.getLogger("web_sys");

  @RequestMapping({"/v_list"})
  public String getAll(String pageNo, ModelMap modelMap, HttpServletRequest request) throws CodeException
  {
    try {
      Page page = this.managerService.getManagers(pageNo);
      modelMap.put("page", page);
      return this.jumpListPagePath;
    } catch (Exception e) {
      logger.error("管理员查询出现系统错误", e);
      throw new CodeException("C0001", e);
    }
  }

  @RequestMapping({"/v_edit"})
  public String viewEditPage(String editId, ModelMap modelMap) throws CodeException
  {
    try {
      AdminUser adminUser = this.managerService.getManagerById(editId);
      modelMap.put("manager", adminUser);
      return this.jumpEditPagePath;
    } catch (Exception e) {
      logger.error("管理员编辑出现系统错误", e);
      throw new CodeException("C0001", e);
    }
  }

  @RequestMapping({"/v_editPassword"})
  public String viewEditPasswordPage(ModelMap modelMap, HttpServletRequest request) throws CodeException
  {
    try {
      AdminUser adminUser = this.managerService.getManagerById(SessionManager.getId(request));
      modelMap.put("manager", adminUser);
      modelMap.put("userOldPassword", PwdEncryptUtil.decrypto(adminUser.getPassword()));
      return this.jumpEditPwdPagePath;
    } catch (Exception e) {
      logger.error("修改密码操作出现系统错误", e);
      throw new CodeException("C0001", e);
    }
  }

  @RequestMapping({"/v_add"})
  public String viewAddPage(ModelMap modelMap) throws CodeException {
    AdminUser mag = new AdminUser();
    mag.setId(StringUtil.generateID());
    modelMap.put("manager", mag);
    return this.jumpAddPagePath;
  }

  @RequestMapping({"/v_detail"})
  public String viewDetailPage(String viewId, ModelMap modelMap, HttpServletRequest request) throws CodeException
  {
    try {
      AdminUser mag = this.managerService.getManagerById(viewId);
      modelMap.put("manager", mag);
      return this.jumpDetailPagePath;
    } catch (Exception e) {
      logger.error("查看管理员详细出现系统错误", e);
      throw new CodeException("C0001", e);
    }
  }

  @RequestMapping({"/o_add"})
  public String add(AdminUser mag, ModelMap modelMap, HttpServletRequest request) throws CodeException
  {
    OperationLog operationLog = OperationLogManager.getInstance(request, this.applicationName, "增加", mag.getName(), "增加管理员信息", null);
    operationLog.setOrganizationId(SessionManager.getManager(request).getOrganizationId());
    try
    {
      mag.setPassword(PwdEncryptUtil.encrypto(mag.getPassword()));
      mag.setLoginErrorTimes(0);
      mag.setUserType(1);
      mag.setLastLoginDate(new Date());
      this.managerService.addManager(mag);
      return this.jumplistdo;
    } catch (CodeException ce) {
      throw new CodeException(ce.getCode(), ce.getMessage());
    } catch (Exception e) {
      operationLog.setResult(10);
      logger.error("管理员添加出现系统错误", e);
      throw new CodeException("C0001", e);
    } finally {
      this.operationLogService.addOperationLog(operationLog);
    }
  }

  @RequestMapping({"/o_edit"})
  public String edit(AdminUser mag, ModelMap modelMap, HttpServletRequest request) throws CodeException
  {
    OperationLog operationLog = OperationLogManager.getInstance(request, this.applicationName, "修改", mag.getName(), "修改管理员信息", null);
    operationLog.setOrganizationId(SessionManager.getManager(request).getOrganizationId());
    try
    {
      AdminUser manager = this.managerService.getManagerById(mag.getId());
      mag.setPassword(manager.getPassword());
      mag.setLoginErrorTimes(0);
      mag.setLastLoginDate(new Date());
      if (manager.getUserType() == 0) {
        mag.setOrganizationId(manager.getOrganizationId());
      }
      this.managerService.update(mag);
      return this.jumplistdo;
    } catch (Exception e) {
      operationLog.setResult(10);
      logger.error("管理员编辑出现系统错误", e);
      throw new CodeException("C0001", e);
    } finally {
      this.operationLogService.addOperationLog(operationLog);
    }
  }

  @RequestMapping({"/o_delete"})
  public String delete(String deleteId, String name, ModelMap modelMap, HttpServletRequest request) throws CodeException
  {
    OperationLog operationLog = OperationLogManager.getInstance(request, this.applicationName, "删除", name, "删除管理员信息", null);
    operationLog.setOrganizationId(SessionManager.getManager(request).getOrganizationId());
    try
    {
      this.managerService.deleteById(deleteId);
      return this.jumplistdo;
    } catch (Exception e) {
      operationLog.setResult(10);
      logger.error("管理员删除出现系统错误", e);
      throw new CodeException("C0001", e);
    } finally {
      this.operationLogService.addOperationLog(operationLog);
    }
  }

  @RequestMapping({"/o_editPassword"})
  public String editPassword(ModelMap map, HttpServletRequest request) throws CodeException
  {
    OperationLog operationLog = OperationLogManager.getInstance(request, "系统管理", "修改", SessionManager.getManager(request).getName(), "修改管理员密码", null);

    operationLog.setOrganizationId(SessionManager.getManager(request).getOrganizationId());
    try
    {
      String newPassword = request.getParameter("newPassword");
      AdminUser manager = this.managerService.getManagerById(SessionManager.getId(request));
      manager.setPassword(PwdEncryptUtil.encrypto(newPassword));
      this.managerService.update(manager);

      map.put("userOldPassword", newPassword);
      map.put("resultInfo", "密码修改成功！");
      return this.jumpEditPwdPagePath;
    } catch (Exception e) {
      operationLog.setResult(10);
      logger.error("管理员密码修改出现系统错误", e);
      throw new CodeException("C0001", e);
    } finally {
      this.operationLogService.addOperationLog(operationLog);
    }
  }
}